package jd.se.message;


import jd.se.common.CurrentUser;
import jd.se.common.Result;
import jd.se.common.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@WebServlet({
        "/sendMessage",
        "/showMessage",
        "/showReturnRequest",
})
public class MessageAPI extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        String path = request.getServletPath();
        if ("/sendMessage".equals(path)){
            request.setCharacterEncoding("UTF-8");
            MessageService messageService = new MessageService();
            // 获取当前用户对象
            CurrentUser currentUser = (CurrentUser)request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);
            String uId = currentUser.getUserId();
            // 获取传送的参数：发送消息的对象
            String toId = request.getParameter("id");
            String mess = request.getParameter("message");
            Timestamp d= new Timestamp(System.currentTimeMillis());
            Message message = new Message(uId, toId, mess, d);
            messageService.insertMessage(message);
        }
        if ("/showMessage".equals(path)){
            CurrentUser currentUser = (CurrentUser)request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);
            String to_id = currentUser.getUserId();
            MessageService messageService = new MessageService();
            Map<String, Object> messageMap = messageService.getMessage(to_id);
            Utils.outResult(resp, Result.success(messageMap));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
